<template>
  <div>
    <el-dialog
      :title="obj.isAdd ? '轮播图添加' : '轮播图编辑'"
      :visible.sync="obj.isTrue"
      width="40%"
      @close="cancel()"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="标题">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <div class="imgBox1">
            <h2>+</h2>
            <input
              type="file"
              name=""
              id=""
              @change="changeImg1"
              v-if="obj.isTrue"
            />
            <img :src="imgUrl" v-if="imgUrl" />
          </div>
        </el-form-item>

        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="add()" v-if="obj.isAdd"
          >添 加</el-button
        >
        <el-button type="primary" @click="enterEdit()" v-else>编 辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { bannerAdd,bannerInfo,bannerEdit} from "../../../request/api";
import { successAlert, warningAlert } from "../../../utils/alert";
export default {
  props: ["obj"],
  components: {},
  data() {
    return {
        imgUrl: "",
        form:{
            title:"",
            status:1,
            img:""
        }
    };
  },
  computed: {
    ...mapGetters({
        bannerList:"banner/list"
        
    }),
  },
  methods: {
    ...mapActions({
        asyncBannerList:"banner/asyncList",
    }),
     changeImg1(e){
       let file = e.target.files[0]
       //限制图片大小
       if(file.size > 2*1024*1024){
        warningAlert("图片大小超出限制")
          return
       }
       let imgType =["image/jpeg","image/jpg","image/webp","image/gif","image/png"]
       if(!imgType.includes(file.type)){
        warningAlert("请上传规定类型图片")
        return
       } 

       //可以将图片信息变成具体图片
       this.imgUrl =URL.createObjectURL(file)
       //给form中img字段赋值
       this.form.img =file
    },
    empty() {
      this.form = {
        title:"",
        status:1,
         img:""
      };
      this.imgUrl=""
    },
    cancel(){
        this.empty()
        this.obj.isTrue = false
    },
    add(){
        bannerAdd(this.form).then(res=>{
        if(res.data.code == 200){
        successAlert(res.data.msg);
        this.empty()
         this.obj.isTrue=false
         this.asyncBannerList()
        }else{
         warningAlert(res.data.msg);
        }
    })
  },
      getOne(id) {
      bannerInfo({ id: id }).then((res) => {
        // 给form赋值
        this.form = res.data.list;
        this.imgUrl = this.$pre + this.form.img
        // 给form添加id字段，因为后面确定编辑的时候需要用到
        this.form.id = id;
      })
    },
     enterEdit(){
    bannerEdit(this.form).then((res) => {
        if (res.data.code == 200) {
          // 成功提示窗
          successAlert(res.data.msg);
          // 关闭当前遮罩层并清空数据
          this.cancel();
          // 重新请求列表数据
          this.asyncBannerList();
        } else {
          // 失败提示窗
          warningAlert(res.data.msg);
        }
      })
    }
  },
  mounted() {},
};
</script>
<style scoped>
.imgBox1{
  width: 150px;
  height: 150px;
  border: 1px dashed #ccc;
}
.imgBox1 h2{
  font-size:40px;
  text-align: center;
  line-height: 150px;
  margin: 0;
}
.imgBox1 input{
  width: 150px;
  height: 150px;
  position: absolute;
  top: 0;
  opacity: 0;
  z-index: 3;
}
.imgBox1 img {
  width: 150px;
  height: 150px;
  position:absolute ;
  top: 0;
  z-index: 2;
}
.dialog-footer{
    width: 180px;
}
</style>